package it.danghongphat.database;


import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JOptionPane;

public class DatabaseManager {
	Statement stm;
	ConnectDatabases conn;
	String query;
	
	public DatabaseManager() {
		openConnect();
	}
	void openConnect(){
		try{
			conn=new ConnectDatabases();
			stm=conn.getConnection().createStatement();
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	public void closeConnection(){
			try {
				if(stm!=null){
				stm.close();
				}
				if(conn!=null){
					conn.getConnection().close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
	}
	public ResultSet getResultSet(String sql){
		ResultSet rs=null;
		try{
			rs=stm.executeQuery(sql);
		}catch(Exception e){
			e.printStackTrace();
		}
		return rs;
	}
	public boolean excuteQuery(String sql){
		try {
			PreparedStatement pst = conn.getConnection().prepareStatement(sql);
			pst.executeQuery();
			return true;
		} catch (SQLException e) {
			System.out.print("Thao tác thêm dữ liệu không thành công");
			JOptionPane.showMessageDialog(null,"Thao tác thêm dữ liệu không thành công."
			+ "\n" + "Nguyên nhân:\n" + e.toString(), "ERROR", JOptionPane.INFORMATION_MESSAGE);
			e.printStackTrace();
			return false;
		}
	}

}
